package archer.framework.protocol.signature.impl;

import archer.framework.protocol.signature.SignatureGenerator;
import archer.framework.utils.AESUtils;
import archer.framework.utils.ValidateUtils;
import jodd.util.Base64;
import org.apache.commons.codec.digest.DigestUtils;

/**
 * @author christ
 * @date 2016/9/7
 */
public class DefaultSignatureGenerator implements SignatureGenerator {

    @Override
    public String generate(byte[] content, byte[] secretKey) {

        if (ValidateUtils.isEmpty(content) || ValidateUtils.isEmpty(secretKey)) {
            return null;
        }

        // 采用sha1对要签名的内容做算法摘要
        byte[] digest = DigestUtils.sha1(content);

        // 采用aes算法对算法摘要做加密
        byte[] encrypt = AESUtils.encrypt(digest, secretKey);

        // base64编码
        return Base64.encodeToString(encrypt);
    }
}
